home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / FIX_DESK / SOURCE / COMMENTS.C < prev    next >
C/C++ Source or Header  |  1988-09-07  |  3KB  |  164 lines

  1. #include <MacTypes.h>
  2. #include <ResourceMgr.h>
  3.  
  4. #include "fix.h"
  5.  
  6.     /* Local types. */
  7.  
  8. typedef struct fcmt_node {
  9.     struct fcmt_node *next;
  10.     int number;
  11. } fcmt_node, *fcmt_ptr, *fcmt_table[HASH_SIZE];
  12.  
  13. static fcmt_table comment_table;
  14.  
  15.     /* Local prototypes. */
  16.  
  17. int hash(int number);
  18. void insert_fcmt(int number);
  19.  
  20.     /* Hash an FCMT index into the hash table range. */
  21.  
  22. static int hash(number)
  23.     int number;
  24. {
  25.     if (number < 0)
  26.         number = -number;
  27.     return(number % HASH_SIZE);
  28. }
  29.  
  30.     /* Insert an FCMT record into the hash table. */
  31.  
  32. static void insert_fcmt(number)
  33.     int number;
  34. {
  35.     register long hash_value = hash(number);
  36.     register fcmt_ptr temp = (fcmt_ptr) NewPtr((long) sizeof(fcmt_node));
  37.  
  38.     temp->next = comment_table[hash_value];    /* Insert at    */
  39.     temp->number = number;                    /* beginning of */
  40.     comment_table[hash_value] = temp;        /* hash list... */
  41. }
  42.  
  43.     /* Remove an FCMT record from the hash table. */
  44.  
  45. void remove_fcmt(number)
  46.     int number;
  47. {
  48.     register long hash_value = hash(number);
  49.     register fcmt_ptr temp, prev;
  50.  
  51.     temp = comment_table[hash_value];
  52.     prev = NIL;
  53.     while ((temp) && (temp->number != number)) {
  54.         prev = temp;
  55.         temp = temp->next;
  56.     }
  57.     if (temp) {            /* Found it... */
  58.         if (!prev)        /* Unlink from chain... */
  59.             comment_table[hash_value] = temp->next;
  60.         else
  61.             prev->next = temp->next;
  62.         DisposPtr(temp);
  63.     }
  64. }
  65.  
  66.     /* Hash a filename to an MFS comment ID (like the Finder). */
  67.     /* The filename MUST be in Pascal format!!!                   */
  68.  
  69. int comment_hash(name)
  70.     register unsigned char *name;
  71. {
  72.     register int value;
  73.  
  74.     asm {
  75.         moveq    #0,d0
  76.         move.b    (name)+,d0
  77.         moveq    #0,value
  78. @1        move.b    (name)+,d1
  79.         eor.b    d1,value
  80.         ror.w    #1,value
  81.         bmi.s    @2
  82.         neg.w    value
  83. @2        subq.w    #1,d0
  84.         bne.s    @1
  85.     }
  86.  
  87.     return (value);
  88. }
  89.  
  90.     /* Perform initial comment processing. */
  91.  
  92. void pre_comment_processing()
  93. {
  94.     register int i, count;
  95.  
  96.         /* Empty the hash table. */
  97.  
  98.     {
  99.         register fcmt_ptr *p;
  100.  
  101.         comment_count = 0;
  102.         p = &comment_table[0];
  103.         for (i=0; i<HASH_SIZE; i++, p++)    /* Clear hash table... */
  104.             *p = NIL;
  105.     }
  106.  
  107.         /* Insert all FCMT resources into the hash table. */
  108.  
  109.     SetResLoad(0);
  110.     count = Count1Resources('FCMT');
  111.     for (i=1; i<=count; i++) {
  112.         register Handle hand;
  113.         int number;
  114.         long blob;
  115.         unsigned char name[LONG_NAME_SIZE];
  116.  
  117.         hand = Get1IndResource('FCMT',i);
  118.         GetResInfo(hand,&number, &blob, &name);
  119.         ReleaseResource(hand);
  120.         insert_fcmt(number);
  121.     }
  122.     if (!is_hfs) {
  123.         count = Count1Resources('FOBJ');    /* Do folders... */
  124.         for (i=1; i<=count; i++) {
  125.             register Handle hand;
  126.             int number;
  127.             long blob;
  128.             unsigned char name[LONG_NAME_SIZE];
  129.  
  130.             hand = Get1IndResource('FOBJ',i);
  131.             GetResInfo(hand, &number, &blob, &name);
  132.             ReleaseResource(hand);
  133.             remove_fcmt(comment_hash(name));
  134.         }
  135.     }
  136.     SetResLoad(1);
  137. }
  138.  
  139.     /* Perform final comment processing. */
  140.  
  141. void post_comment_processing()
  142. {
  143.     register int i;
  144.     register fcmt_ptr *p;
  145.  
  146.         /* For each entry left in the hash table, remove its FCMT resource. */
  147.  
  148.     p = &comment_table[0];
  149.     for (i=0; i<HASH_SIZE; i++, p++) {
  150.         register fcmt_ptr temp, temp2;
  151.  
  152.         temp = *p;
  153.         while (temp) {
  154.             comment_count++;
  155. #ifndef TEST_MODE
  156.             kill_resource('FCMT', temp->number);
  157. #endif TEST_MODE
  158.             temp2 = temp;
  159.             temp = temp->next;
  160.             DisposPtr(temp2);
  161.         }
  162.     }
  163. }
  164.